% CALCULATE 'AVERAGE' VELOCITY (from 0ms to start of 1st occlusion)
% for fast speed

% target22: occluder present target22.plotocc.x(1,1)ms to target22.plotocc.x(1,2)ms 
% & target22.plotocc.x(2,1)ms to target22.plotocc.x(2,2)ms

% equiv. (i.e. same part of phase) non-occluded period = 
% eqocc22ms to target22.plotocc.x(1,2)ms 
eqocc22 = msPerhCycle - target22.plotocc.x(1,2);

% NB initial delay (200ms) in the occluder is omitted in calculating the 
% resid pred gain, therefore it must be omitted from the equivalent
% non-occluded period

PlotNoisy.avgvel = mean(PlotNoisy.meanvelnosac(eqocc22:(target22.plotocc.x(1,1)-delay),:));
PlotSmooth.avgvel = mean(PlotSmooth.meanvelnosac(eqocc22:(target22.plotocc.x(1,1)-delay),:));

target22.avgvel = mean(target22.Xvel(eqocc22:(target22.plotocc.x(1,1)-delay),:));

% CALCULATE CLOSED LOOP GAIN (mean eye velocity / mean target velocity)

PlotNoisy.gain = PlotNoisy.avgvel/target22.avgvel;
PlotSmooth.gain = PlotSmooth.avgvel/target22.avgvel;

% CALCULATE MEAN VELOCITY ERROR (for equiv path to occluded part)
% = mean of all the diffs between eye (w/out saccs) and target velocity

PlotNoisy.velerr = mean(abs(target22.onevel(eqocc22:(target22.plotocc.x(1,1)-delay),:) - ...
    PlotNoisy.meanvelnosac(eqocc22:(target22.plotocc.x(1,1)-delay),:)));
PlotSmooth.velerr = mean(abs(target22.onevel(eqocc22:(target22.plotocc.x(1,1)-delay),:) - ...
    PlotSmooth.meanvelnosac(eqocc22:(target22.plotocc.x(1,1)-delay),:)));

% CALCULATE MEAN RESIDUAL PREDICTIVE VELOCITIES (mean velocity behind occluder
% discounting an inital delay as eye velocity is maintained for this
% period)
               
% rpv(1) = rpv in 1st occlusion (without negative sign)

PlotNoisy.rpv(1) = -mean(PlotNoisy.meanvelnosac((target22.plotocc.x(1,1)+delay):...
    target22.plotocc.x(1,2),:));
PlotSmooth.rpv(1) = -mean(PlotSmooth.meanvelnosac((target22.plotocc.x(1,1)+delay):...
    target22.plotocc.x(1,2),:));

target22.avgoccvel(1) = -mean(target22.Xvel((target22.plotocc.x(1,1)...
    +delay):target22.plotocc.x(1,2),:));

% rpv(2) = rpv in 2nd occlusion

PlotNoisy.rpv(2) = mean(PlotNoisy.meanvelnosac((target22.plotocc.x(2,1)+delay):...
    target22.plotocc.x(2,2),:));
PlotSmooth.rpv(2) = mean(PlotSmooth.meanvelnosac((target22.plotocc.x(2,1)+delay):...
    target22.plotocc.x(2,2),:));

target22.avgoccvel(2) = mean(target22.Xvel((target22.plotocc.x(2,1)...
    +delay):target22.plotocc.x(2,2),:));

% CALCULATE RESIDUAL PREDICTIVE GAINS

PlotNoisy.rpg(1) = PlotNoisy.rpv(1)/target22.avgoccvel(1);
PlotSmooth.rpg(1) = PlotSmooth.rpv(1)/target22.avgoccvel(1);

PlotNoisy.rpg(2) = PlotNoisy.rpv(2)/target22.avgoccvel(2);
PlotSmooth.rpg(2) = PlotSmooth.rpv(2)/target22.avgoccvel(2);

% CALCULATE MEAN VELOCITY ERROR FOR OCCLUDED PARTS

% rve(1) = resid mean velocity error in 1st occlusion (decelerating)

PlotNoisy.rve(1) = mean(abs(target22.onevel((target22.plotocc.x(1,1)+delay):target22.plotocc.x(1,2),:) - ...
    PlotNoisy.meanvelnosac((target22.plotocc.x(1,1)+delay):target22.plotocc.x(1,2),:)));
PlotSmooth.rve(1) = mean(abs(target22.onevel((target22.plotocc.x(1,1)+delay):target22.plotocc.x(1,2),:) - ...
    PlotSmooth.meanvelnosac((target22.plotocc.x(1,1)+delay):target22.plotocc.x(1,2),:)));

% rve(2) = resid mean velocity error in 2nd occlusion (accelerating)

PlotNoisy.rve(2) = mean(abs(target22.onevel((target22.plotocc.x(2,1)+delay):target22.plotocc.x(2,2),:) - ...
    PlotNoisy.meanvelnosac((target22.plotocc.x(2,1)+delay):target22.plotocc.x(2,2),:)));
PlotSmooth.rve(2) = mean(abs(target22.onevel((target22.plotocc.x(2,1)+delay):target22.plotocc.x(2,2),:) - ...
    PlotSmooth.meanvelnosac((target22.plotocc.x(2,1)+delay):target22.plotocc.x(2,2),:)));

% PUT GAINS & VELOCITY ERRORS IN PLOTABLE FORM

Stats.gain(:,1) = [PlotNoisy.gain, PlotSmooth.gain];
Stats.gain(:,2) = [PlotNoisy.rpg(2), PlotSmooth.rpg(2)];
Stats.gain(:,3) = [PlotNoisy.rpg(1), PlotSmooth.rpg(1)];

Stats.velerr(:,1) = [PlotNoisy.velerr, PlotSmooth.velerr];
Stats.velerr(:,2) = [PlotNoisy.rve(2), PlotSmooth.rve(2)];
Stats.velerr(:,3) = [PlotNoisy.rve(1), PlotSmooth.rve(1)];